#!/bin/bash
 
APP_NAME="${0##*[\\/]}"
APP_VERSION="1.0"
 
cRed=1
cGreen=2
cYellow=3
cBlue=4
cFuchsia=5
cCyan=6
cWhite=7
 
colorTable=({1..7})
 
iLeft=3
iTop=2
 
((iTrayLeft=iLeft+2))
((iTrayTop=iTop+1))
((iTrayWidth=10))
((iTrayHeight=15))
 
cScore=$cRed
cScoreValue=$cBlue
 
sigRotate=25
sigLeft=26
sigRight=27
sigDown=28
sigAllDown=29
sigExit=30
 
box0=(0 0 0 1 1 0 1 1)
box1=(0 2 1 2 2 2 3 2 1 0 1 1 1 2 1 3)
box2=(0 0 0 1 1 1 1 2 0 1 1 0 1 1 2 0)
box3=(0 1 0 2 1 0 1 1 0 0 1 0 1 1 2 1) #
box4=(0 1 0 2 1 1 2 1 1 0 1 1 1 2 2 2 0 1 1 1 2 0 2 1 0 0 1 0 1 1 1 2)
box5=(0 1 1 1 2 1 2 2 1 0 1 1 1 2 2 0 0 0 0 1 1 1 2 1 0 2 1 0 1 1 1 2)
box6=(0 1 1 1 1 2 2 1 1 0 1 1 1 2 2 1 0 1 1 0 1 1 2 1 0 1 1 0 1 1 1 2)
box=($(eval echo \${box{0..6}[@]}))
countBox=(1 2 2 2 4 4 4)
offsetBox=(0 1 3 5 7 11 15) # 8 length
iScoreEachLevel=500
sig=0
iScore=0
iLevel=0
boxNew=()
cBoxNew=0
iBoxNewType=0
iBoxNewRotate=0
boxCur=()
cBoxCur=0
iBoxCurType=0
iBoxCurRotate=0
boxCurX=-1
boxCurY=-1
iMap=()               
 
for((i=0; i<iTrayHeight*iTrayWidth; ++i)); do iMap[i]=-1; done
 
tstp()
{
   kill -s STOP ${1} &> /dev/null
}
 
cont()
{
   kill -s CONT ${1} &> /dev/null
}
 
RunAsKeyReceiver()
{
        local pidDisplayer key aKey sig cESC sTTY
        pidDisplayer=$1
        aKey=(0 0 0)
        cESC=$(echo -ne "\e")
        cSpace=$(echo -ne "\040")
        sTTY=$(stty -g)
        trap "MyExit;" INT TERM
        trap "MyExitNoSub;" $sigExit
        echo -ne "\e[?25l"
 
        while :
        do
                read -s -n 1 key
                aKey[0]=${aKey[1]}
                aKey[1]=${aKey[2]}
                aKey[2]=$key
                sig=0
                if [[ $key == $cESC && ${aKey[1]} == $cESC ]]
                then
                        MyExit
                elif [[ "[${key}]" == "[]" ]]; then sig=${sigAllDown}
                elif [[ ${aKey[0]} == $cESC && ${aKey[1]} == "[" ]]; then
 
                          case ${key} in
                          A)    sig=${sigRotate}    ;;
                          B)    sig=${sigDown}      ;;
                          D)    sig=${sigLeft}      ;;
                          C)    sig=${sigRight}     ;;
                          esac
                else
                          case ${key} in
                          W|w)  sig=${sigRotate}    ;;
                          S|s)  sig=${sigDown}      ;;
                          A|a)  sig=${sigLeft}      ;;
                          D|d)  sig=${sigRight}     ;;
                          P|p)  pause ${pid}  0     ;;
                          R|r)  pause ${pid}  1     ;;
                          Q|q)  quit                ;;
                          esac
                fi
 
                if [[ $sig != 0 ]]
                then
                        kill -$sig $pidDisplayer
                fi
        done
}
 
MyExitNoSub()
{
        local y
        stty $sTTY
        ((y=iTop+iTrayHeight+4))
        echo -e "\e[?25h\e[${y};9H"
        exit
}
 
MyExit()
{
        kill -$sigExit $pidDisplayer
        MyExitNoSub
}
 
RunAsDisplayer()
{
        local sigThis
        InitDraw
        trap "sig=$sigRotate;" $sigRotate
        trap "sig=$sigLeft;" $sigLeft
        trap "sig=$sigRight;" $sigRight
        trap "sig=$sigDown;" $sigDown
        trap "sig=$sigAllDown;" $sigAllDown
        trap "ShowExit;" $sigExit
        while :
        do
                for ((i = 0; i < 21 - iLevel; i++))
                do
                        sleep 0.02
                        sigThis=$sig
                        sig=0
                        case ${sigThis} in
                        ${sigRotate})  BoxRotate  ;;
                        ${sigLeft} )   BoxLeft    ;;
                        ${sigRight})   BoxRight   ;;
                        ${sigDown})    BoxDown    ;;
                        ${sigAllDown}) BoxAllDown ;;
                        esac
                done
                BoxDown
        done
}
 
BoxMove()
{
        local j i x y xTest yTest
        yTest=$1
        xTest=$2
        for ((j = 0; j < 8; j += 2))
        do
                ((i = j + 1))
                ((y = ${boxCur[$j]} + yTest))
                ((x = ${boxCur[$i]} + xTest))
                if (( y < 0 || y >= iTrayHeight || x < 0 || x >= iTrayWidth))
                then
                        return 1
                fi
                if ((${iMap[y * iTrayWidth + x]} != -1 ))
                then
                        return 1
                fi
        done
        return 0;
}
 
Box2Map()
{
        local j i x y xp yp line
        for ((j = 0; j < 8; j += 2))
        do
                ((i = j + 1))
                ((y = ${boxCur[$j]} + boxCurY))
                ((x = ${boxCur[$i]} + boxCurX))
                ((i = y * iTrayWidth + x))
                iMap[$i]=$cBoxCur
        done
        line=0
        for ((j = 0; j < iTrayWidth * iTrayHeight; j += iTrayWidth))
        do
                for ((i = j + iTrayWidth - 1; i >= j; i--))
                do
                        if ((${iMap[$i]} == -1)); then break; fi
                done
                if ((i >= j)); then continue; fi
                ((line++))
                for ((i = j - 1; i >= 0; i--))
                do
                        ((x = i + iTrayWidth))
                        iMap[$x]=${iMap[$i]}
                done
                for ((i = 0; i < iTrayWidth; i++))
                do
                        iMap[$i]=-1
                done
        done
        if ((line == 0)); then return; fi
        ((x = iLeft + iTrayWidth * 2 + 7))
        ((y = iTop + 6))
        ((iScore += line * 10 - 5))
        echo -ne "\e[1m\e[3${cScoreValue}m\e[${y};${x}H${iScore}         "
        if ((iScore % iScoreEachLevel < line * 2 - 1))
        then
                if ((iLevel < 20))
                then
                        ((iLevel++))
                        ((y = iTop + 8))
                        echo -ne "\e[3${cScoreValue}m\e[${y};${x}H${iLevel}        "
                fi
        fi
        echo -ne "\e[0m"
        for ((y = 0; y < iTrayHeight; y++))
        do
                ((yp = y + iTrayTop + 1))
                ((xp = iTrayLeft + 1))
                ((i = y * iTrayWidth))
                echo -ne "\e[${yp};${xp}H"
                for ((x = 0; x < iTrayWidth; x++))
                do
                        ((j = i + x))
                        if ((${iMap[$j]} == -1))
                        then
                                echo -ne "  "
                        else
                                if (( ${iMap[$j]} == 7 )); then
                                       echo -ne "\e[1;37m\e[1;40m[]\e[0m"
                                else
                                       echo -ne "\e[1;3${iMap[$j]}m\e[1;4${iMap[$j]}m[]\e[0m"
                                fi
                        fi
                done
        done
}
 
BoxDown()
{
        local y s
        ((y = boxCurY + 1))
        if BoxMove $y $boxCurX
        then
                s="`DrawCurBox 0`"
                ((boxCurY = y))
                s="$s$(DrawCurBox 1)"
                echo -ne ${s}
        else
                Box2Map
                RandomBox
        fi
}
 
BoxLeft()
{
        local x s
        ((x = boxCurX - 1))
        if BoxMove $boxCurY $x
        then
                s=$(DrawCurBox 0)
                ((boxCurX = x))
                s=$s$(DrawCurBox 1)
                echo -ne $s
        fi
}
 
BoxRight()
{
        local x s
        ((x = boxCurX + 1))
        if BoxMove $boxCurY $x
        then
                s=`DrawCurBox 0`
                ((boxCurX = x))
                s=$s`DrawCurBox 1`
                echo -ne $s
        fi
}
 
BoxAllDown()
{
        local k j i x y iDown s
        iDown=$iTrayHeight
        for ((j = 0; j < 8; j += 2))
        do
                ((i = j + 1))
                ((y = ${boxCur[$j]} + boxCurY))
                ((x = ${boxCur[$i]} + boxCurX))
                for ((k = y + 1; k < iTrayHeight; k++))
                do
                        ((i = k * iTrayWidth + x))
                        if (( ${iMap[$i]} != -1)); then break; fi
                done
                ((k -= y + 1))
                if (( $iDown > $k )); then iDown=$k; fi
        done
        s=`DrawCurBox 0`
        ((boxCurY += iDown))
        s=$s`DrawCurBox 1`
        echo -ne $s
        Box2Map
        RandomBox
}
 
BoxRotate()
{
        local iCount iTestRotate boxTest j i s
        iCount=${countBox[$iBoxCurType]}
        ((iTestRotate = iBoxCurRotate + 1))
        if ((iTestRotate >= iCount))
        then
                ((iTestRotate = 0))
        fi
        for ((j = 0, i = (${offsetBox[$iBoxCurType]} + $iTestRotate) * 8; j < 8; j++, i++))
        do
                boxTest[$j]=${boxCur[$j]}
                boxCur[$j]=${box[$i]}
        done
        if BoxMove $boxCurY $boxCurX
        then
                for ((j = 0; j < 8; j++))
                do
                        boxCur[$j]=${boxTest[$j]}
                done
                s=`DrawCurBox 0`
                for ((j = 0, i = (${offsetBox[$iBoxCurType]} + $iTestRotate) * 8; j < 8; j++, i++))
                do
                        boxCur[$j]=${box[$i]}
                done
                s=$s`DrawCurBox 1`
                echo -ne $s
                iBoxCurRotate=$iTestRotate
        else
                for ((j = 0; j < 8; j++))
                do
                        boxCur[$j]=${boxTest[$j]}
                done
        fi
}
 
DrawCurBox()
{
        local i j t bDraw sBox s
        bDraw=$1
        s=""
        if (( bDraw == 0 ))
        then
                sBox="\040\040"
        else
                sBox="[]"
                if (( ${cBoxCur} == 7 )); then
                       s=$s"\e[1;37m\e[1;40m"
                else
                       s=$s"\e[1;3${cBoxCur}m\e[1;4${cBoxCur}m"
                fi
        fi
        for ((j = 0; j < 8; j += 2))
        do
                ((i = iTrayTop + 1 + ${boxCur[$j]} + boxCurY))
                ((t = iTrayLeft + 1 + 2 * (boxCurX + ${boxCur[$j + 1]})))
                s=$s"\e[${i};${t}H${sBox}"
        done
        s=$s"\e[0m"
        echo -n $s
}
 
RandomBox()
{
        local i j t
        iBoxCurType=${iBoxNewType}
        iBoxCurRotate=${iBoxNewRotate}
        cBoxCur=${cBoxNew}
        for ((j = 0; j < ${#boxNew[@]}; j++))
        do
                boxCur[$j]=${boxNew[$j]}
        done
        if (( ${#boxCur[@]} == 8 ))
        then
                for ((j = 0, t = 4; j < 8; j += 2))
                do
                        if ((${boxCur[$j]} < t)); then t=${boxCur[$j]}; fi
                done
                ((boxCurY = -t))
                for ((j = 1, i = -4, t = 20; j < 8; j += 2))
                do
                        if ((${boxCur[$j]} > i)); then i=${boxCur[$j]}; fi
                        if ((${boxCur[$j]} < t)); then t=${boxCur[$j]}; fi
                done
                ((boxCurX = (iTrayWidth - 1 - i - t) / 2))
                echo -ne `DrawCurBox 1`
                if ! BoxMove $boxCurY $boxCurX
                then
                        kill -$sigExit ${PPID}
                        ShowExit
                fi
        fi
        for ((j = 0; j < 4; j++))
        do
                ((i = iTop + 1 + j))
                ((t = iLeft + 2 * iTrayWidth + 7))
                echo -ne "\e[${i};${t}H        "
        done
        ((iBoxNewType = RANDOM % ${#offsetBox[@]}))
        ((iBoxNewRotate = RANDOM % ${countBox[$iBoxNewType]}))
        for ((j = 0, i = (${offsetBox[$iBoxNewType]} + $iBoxNewRotate) * 8; j < 8; j++, i++))
        do
                boxNew[$j]=${box[$i]};
        done
        ((cBoxNew = ${colorTable[RANDOM % ${#colorTable[@]}]}))
        if (( ${cBoxNew} == 7 )); then
               echo -ne "\e[1;37m\e[1;40m"
        else
               echo -ne "\e[1;3${cBoxNew}m\e[1;4${cBoxNew}m"
        fi
        for ((j = 0; j < 8; j += 2))
        do
                ((i = iTop + 1 + ${boxNew[$j]}))
                ((t = iLeft + 2 * iTrayWidth + 7 + 2 * ${boxNew[$j + 1]}))
                echo -ne "\e[${i};${t}H[]"
        done
        echo -ne "\e[0m"
}
 
InitDraw()
{
        clear
        RandomBox
        RandomBox
        local i t1 t2 t3
        #echo -ne "\e[1;30;1;44m\e[7m[]\e[m"
        #echo -ne "\e[1;36m\e[1;40m"
        ((t2 = iLeft + 1))
        ((t3 = iLeft + iTrayWidth * 2 + 3))
        for ((i = 0; i < iTrayHeight; i++))
        do
                ((t1 = i + iTop + 2))
                echo -ne "\e[1;30;1;44m\e[7m\e[${t1};${t2}H||"
                echo -ne "\e[1;30;1;44m\e[7m\e[${t1};${t3}H||"
        done
        ((t2 = iTop + iTrayHeight + 2))
        for ((i = 0; i < iTrayWidth + 2; i++))
        do
                ((t1 = i * 2 + iLeft + 1))
                echo -ne "\e[${iTrayTop};${t1}H=="
                echo -ne "\e[${t2};${t1}H=="
        done
        echo -ne "\e[0m"
        echo -ne "\e[1m"
        ((t1 = iLeft + iTrayWidth * 2 + 7))
        ((t2 = iTop + 5))
        echo -ne "\e[1;3${cScore}m\e[${t2};${t1}HScore"
        ((t2 = iTop + 6))
        echo -ne "\e[1;3${cScoreValue}m\e[${t2};${t1}H${iScore}"
        ((t2 = iTop + 7))
        echo -ne "\e[1;3${cScore}m\e[${t2};${t1}HLevel"
        ((t2 = iTop + 8))
        echo -ne "\e[1;3${cScoreValue}m\e[${t2};${t1}H${iLevel}"
        echo -ne "\e[0m"
        ((t2 = iTop + 10))
        echo -ne "\e[${t2};${t1}Hp|P         -  pause"
        ((t2 = iTop + 11))
        echo -ne "\e[${t2};${t1}Hr|R         -  resume"
        ((t2 = iTop + 12))
        echo -ne "\e[${t2};${t1}Hw|W|up      -  rotate"
        ((t2 = iTop + 13))
        echo -ne "\e[${t2};${t1}Hs|S|down    -  one step down"
        ((t2 = iTop + 14))
        echo -ne "\e[${t2};${t1}Ha|A|left    -  one step left"
        ((t2 = iTop + 15))
        echo -ne "\e[${t2};${t1}Hd|D|right   -  one step right"
        ((t2 = iTop + 16))
        echo -ne "\e[${t2};${t1}Hspace|enter -  drop all down"
        echo -ne "\e[0m"
}
 
ShowExit()
{
        local y
        ((y = iTrayHeight + iTrayTop + 3))
        echo -e "\e[${y};11HGame Over!\e[0m"
        exit
}
 
Usage()
{
        cat << EOF
Usage: $APP_NAME
Start tetris game.
  -h, --help              display this help and exit
      --version           output version information and exit
EOF
}
 
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
        Usage
elif [[ "$1" == "--version" ]]; then
        echo "$APP_NAME $APP_VERSION"
elif [[ "$1" == "--show" ]]; then
        RunAsDisplayer
else
        bash $0 --show&
        echo $!
        RunAsKeyReceiver $!
fi
